const {
  $util,
} = getApp();
const MAX_LENGTH = 10;
Page({
  data: {
    idx: '',
    page: 1,
    commentList: [],
    hasMore: true,
    loaded: false,
    showEvalutionBorder: false, // 评论框
    isShare: false,
    ishide: false, // 是否显示阅读更多
    isshow: false, // 是否已经显示过阅读更多
    jump: false,
    imgUrl: $util.getImgUrl(),
  },
  showLoading: true, // 是否显示加载中
  isArticleLike: true, // 是否允许文章点赞
  isCommentLike: true, // 是否允许评论点赞
  isArticleCollect: true, // 是否允许文章收藏
  onLoad(opts) {
    const pages = getCurrentPages(); // 当前页面
    this.setData({
      idx: opts.idx || '',
      id: opts.id,
      isShare: pages.length === 1,
    });
    this.getDetail(opts.id);
  },
  onShow() {
    let sto = wx.getStorageSync('webSto');
    if (sto) {
      wx.navigateBack({
        delta: 1,
      });
      wx.clearStorageSync('webSto');
    }

    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },
  catchmove(e) {
    if (!this.data.isshow) {
      wx.createSelectorQuery().select('#top').boundingClientRect((rec) => {
        if (rec && rec.height > 1000) {
          this.setData({
            ishide: true,
          });
        }
      }).exec();
    }
  },
  hidemore() {
    this.setData({
      ishide: false,
      isshow: true,
    });
  },
  getDetail(id) {
    $util.request({
      url: 'entry/wxapp/information.news.detail',
      data: {
        id,
      },
      success: (res) => {
        if (Number(res.data.iswechatnews) === 1) {
          this.setData({
            jump: true,
          });
          let url = `/ws_party_const/pages/web/web?webview_url=${res.data.wechatlink}`;
          wx.navigateTo({
            url,
          });
        } else {
          this.getCommentList();
          this.setData(res.data);
        }
      },
    });
  },
  getCommentList() {
    $util.request({
      url: 'entry/wxapp/information.comment.commentslist',
      data: {
        id: this.data.id,
        page: this.data.page,
      },
      showLoading: this.showLoading,
      success: (res) => {
        let commentList = res.data;
        if (this.data.page > 1) {
          commentList = [...this.data.commentList, ...commentList];
        }
        this.setData({
          commentList,
          hasMore: res.data.length === MAX_LENGTH,
          loaded: !!(this.data.page !== 1 && res.data.length !== MAX_LENGTH),
        });
      },
    });
  },
  // 编辑评论
  evalate(e) {
    this.setData({
      commentid: e.currentTarget.dataset.commentid || '',
      showEvalutionBorder: !this.data.showEvalutionBorder,
    });
  },
  bindIputval(e) {
    this.setData({
      content: e.detail.value,
    });
  },
  bindlinechange(e) {
    this.setData({
      heightRpx: e.detail.heightRpx,
    });
  },
  isprevent: false,
  // 添加评论
  addComment(e) {
    if (!this.data.content) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
      });
      return;
    }
    if (this.isprevent) return;
    this.isprevent = true;
    const data = {
      id: this.data.id,
      content: this.data.content,
      type: 1,
    };
    if (this.data.commentid) {
      data.type = 2;
      data.refid = this.data.commentid;
    }
    $util.request({
      url: 'entry/wxapp/information.comment.addcomments',
      data,
      success: (res) => {
        wx.showToast({
          title: this.data.commentid ? '回复成功' : '评论成功',
          icon: 'success',
          duration: 1500,
        });
        setTimeout(() => {
          this.setData({
            content: '',
            page: 1,
            hasMore: true,
            loaded: false,
            showEvalutionBorder: false,
            commentnum: this.data.commentid ? this.data.commentnum : ++this.data.commentnum,
          });
          this.getCommentList();
        }, 1500);
      },
      complete: () => {
        this.isprevent = false;
      },
    });
  },
  // 删除评论
  deteleComment(e) {
    const list = this.data.commentList;
    const idx = e.currentTarget.dataset.idx;
    const id = e.currentTarget.dataset.id;
    const isrelist = e.currentTarget.dataset.isrelist;
    let cid = '';
    wx.showModal({
      content: '是否删除该评论？',
      confirmColor: '#5685BB',
      cancelColor: '#333333',
      confirmText: '确认',
      success: (res) => {
        if (res.confirm) {
          $util.request({
            url: 'entry/wxapp/information.comment.delselfcomments',
            data: {
              id,
            },
            success: () => {
              if (Number(isrelist) !== 0) {
                for (const i in list) {
                  if (Number(isrelist) === Number(list[i].id)) {
                    cid = i;
                    break;
                  }
                }
                list[cid].relist.splice(idx, 1);
              } else {
                list.splice(idx, 1);
              }
              this.setData({
                commentList: list,
              });
            },
          });
        }
      },
    });
  },
  // 评论点赞
  opreateComment(e) {
    const idx = e.currentTarget.dataset.idx;
    if (!this.isCommentLike) return;
    this.isCommentLike = false;
    $util.request({
      url: 'entry/wxapp/information.comment.commentlike',
      data: {
        id: this.data.id,
        commentid: e.currentTarget.dataset.id,
      },
      showLoading: false,
      success: (res) => {
        const iscommentlike = `commentList[${idx}].iscommentlike`;
        const fcounts = `commentList[${idx}].fcounts`;
        this.setData({
          [iscommentlike]: Number(res.code),
          [fcounts]: Number(res.allnum),
        });
      },
      complete: () => {
        this.isCommentLike = true;
      },
    });
  },
  // 文章点赞
  operateLike() {
    if (!this.isArticleLike) return;
    this.isArticleLike = false;
    $util.request({
      url: 'entry/wxapp/information.news.newslike',
      data: {
        id: this.data.id,
      },
      showLoading: false,
      success: (res) => {
        this.setData({
          islike: Number(res.code),
          likenum: Number(res.allnum),
        });
      },
      complete: () => {
        this.isArticleLike = true;
      },
    });
  },
  // 文章收藏
  operateCollect() {
    if (!this.isArticleCollect) return;
    this.isArticleCollect = false;
    $util.request({
      url: 'entry/wxapp/information.news.newsCollect',
      data: {
        id: this.data.id,
      },
      showLoading: false,
      success: (res) => {
        this.setData({
          iscollect: Number(res.code),
          collectnum: Number(res.allnum),
        });
      },
      complete: () => {
        this.isArticleCollect = true;
      },
    });
  },
  scrollComment() {
    let windowHeight = wx.systemInfo.windowHeight;
    let commentTop;
    let height;
    wx.createSelectorQuery().select('#comment').boundingClientRect((rect) => {
      commentTop = rect.top;
      wx.createSelectorQuery().select('#top').boundingClientRect((rec) => {
        height = rec.height;
        if (commentTop > windowHeight) {
          wx.pageScrollTo({
            scrollTop: height,
            duration: 300,
          });
        }
      }).exec();
    }).exec();
  },
  onReachBottom() {
    if (!this.data.hasMore) return;
    this.setData({
      page: this.data.page + 1,
    }, () => {
      this.getCommentList();
    });
  },
  onShareAppMessage(){},
});